//
//  LikeViewController.m
//  Molian
//
//  Created by 胡津 on 1/7/15.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "LikeViewController.h"
#import "LikeUserViewController.h"
#import "LikeRecommendedViewController.h"

#import "SwitchView.h"

@interface LikeViewController ()<UIScrollViewDelegate,SwitchViewDelegate>
@property (nonatomic,strong) UIScrollView *scrollView;
@property LikeUserViewController *likeUser;
@property LikeRecommendedViewController *likerecommend;
@property NSInteger currentIndex;
@property float bgViewHeight;

@end

@implementation LikeViewController
{
    SwitchView *_switchView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
       
    CGFloat width = 100;
    if (IS_IPHONE_6){
        width = 90;
    }else if (IS_IPHONE_6_PLUS){
        width = 110;
    }else{
        width = 70;
    }
    
    _switchView = [[SwitchView alloc] initWithFrame:CGRectMake( 0, 0, DeviceWidth - width*2, titleHeight)];
    
    //_switchView = [[SwitchView alloc] initWithFrame:CGRectMake( width, 0, DeviceWidth-100, titleHeight)];
    _switchView.delegate = self;
    _switchView.TitleArr = @[@"喜欢的人", @"  喜欢的推荐"];
    [_switchView initview];
//    _switchView.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.4];
//    self.navigationItem.titleView = _switchView;
    //[view addSubview:_switchView];
    self.navigationItem.titleView = _switchView;
    //视图高度
    self.bgViewHeight = DeviceHeight;
    
    //初始化滑动视图
    [self initScroll];
    //初始化VC
    [self initViewController];
    
}

-(void)initScroll
{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, DeviceWidth, self.bgViewHeight)];
    self.scrollView.alwaysBounceHorizontal = YES;
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = YES;
    self.scrollView.delegate = self;
    self.scrollView.userInteractionEnabled = YES;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.scrollsToTop = NO;
    [self.view addSubview:self.scrollView];
    
    [self.scrollView setContentSize:CGSizeMake(DeviceWidth * (2), DeviceHeight)];
    //    [self.scrollView setContentOffset:CGPointMake(0*DeviceWidth, 0)animated:YES];
    
    
}

-(void)initViewController
{
    self.likeUser = [[LikeUserViewController alloc]init];
    self.likeUser.view.frame = CGRectMake(0, 0, DeviceWidth, self.bgViewHeight-64);
    [self.scrollView addSubview:self.likeUser.view];
    [self addChildViewController:self.likeUser];
    
    self.likerecommend = [[LikeRecommendedViewController alloc]init];
    self.likerecommend.view.frame = CGRectMake(DeviceWidth, 0, DeviceWidth, self.bgViewHeight-64);
    [self.scrollView addSubview:self.likerecommend.view];
    [self addChildViewController:self.likerecommend];
    
    
    __weak __typeof(self) weakSelf = self;
    _switchView.ButtonActionBlock = ^(NSInteger buttonTag){
        
        weakSelf.currentIndex = buttonTag-100;
        
        POPSpringAnimation *positionAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPScrollViewContentOffset];
        positionAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(DeviceWidth* (weakSelf.currentIndex-1),0)];
        positionAnimation.springBounciness = 8.f;
        [weakSelf.scrollView pop_addAnimation:positionAnimation forKey:@"layerPositionAni2mation"];
    };
    
    self.currentIndex = 1;
}

//通知滑动了页面
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGFloat pagewidth = self.scrollView.frame.size.width;
    int currentPage = floor((self.scrollView.contentOffset.x - pagewidth/ (2)) / pagewidth) + 1;
    self.currentIndex = currentPage+1;
    [_switchView swipeAction:(100+currentPage+1)];
}


//滑动条滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.scrollView.contentOffset.x != 0) {
        [_switchView sliding:self.scrollView.contentOffset.x];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
